package com.ruoyi.tea.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cc.casually.htmlParse.http.HttpClient;
import cc.casually.htmlParse.http.Request;
import cc.casually.htmlParse.http.Response;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.tea.cache.ConfCache;
import com.ruoyi.tea.conf.SystemConf;
import com.ruoyi.tea.util.WxChatUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tea.domain.TeaRoom;
import com.ruoyi.tea.service.ITeaRoomService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 直播间信息Controller
 *
 * @author ruoyi
 * @date 2022-07-31
 */
@Controller
@RequestMapping("/tea/room")
public class TeaRoomController extends BaseController {
    private String prefix = "tea/room";

    @Autowired
    private ITeaRoomService teaRoomService;

    @RequiresPermissions("tea:room:view")
    @GetMapping()
    public String room() {
        return prefix + "/room";
    }

    /**
     * 查询直播间信息列表
     */
    @RequiresPermissions("tea:room:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeaRoom teaRoom) {
        startPage();
        List<TeaRoom> list = teaRoomService.selectTeaRoomList(teaRoom);
        return getDataTable(list);
    }

    /**
     * 导出直播间信息列表
     */
    @RequiresPermissions("tea:room:export")
    @Log(title = "直播间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeaRoom teaRoom) {
        List<TeaRoom> list = teaRoomService.selectTeaRoomList(teaRoom);
        ExcelUtil<TeaRoom> util = new ExcelUtil<TeaRoom>(TeaRoom.class);
        return util.exportExcel(list, "直播间信息数据");
    }

    /**
     * 新增直播间信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存直播间信息
     */
    @RequiresPermissions("tea:room:add")
    @Log(title = "直播间信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeaRoom teaRoom) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SysUser sysUser = getSysUser();
        teaRoom.setRoomCode(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "-" + sysUser.getUserId());
        //TODO 调用微信小程序创建直播间接口创建直播间
        //TODO 调用文件上传接口获取文件ID
        Request request = new Request();
        request.setUri("https://api.weixin.qq.com/wxaapi/broadcast/room/create");
        request.addParam("access_token", ConfCache.ACCESS_TOKEN);
        request.addBody("name", teaRoom.getName());
        request.addBody("coverImg", WxChatUtil.fileToMedia(SystemConf.LOCAL_FILE_PATH + teaRoom.getCoverImg().split("profile")[0]));
        try {
            request.addBody("startTime", sdf.parse(teaRoom.getStartTime()).getTime());
            request.addBody("endTime", sdf.parse(teaRoom.getEndTime()).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        request.addBody("anchorName",teaRoom.getAnchorName());
        request.addBody("anchorWechat",teaRoom.getAnchorWechat());
        request.addBody("shareImg",WxChatUtil.fileToMedia(SystemConf.LOCAL_FILE_PATH + teaRoom.getShareImg().split("profile")[0]));
        request.addBody("feedsImg",WxChatUtil.fileToMedia(SystemConf.LOCAL_FILE_PATH + teaRoom.getFeedsImg().split("profile")[0]));
        request.addBody("type",0);
        request.addBody("closeLike",0);
        request.addBody("closeGoods",0);
        request.addBody("closeComment",0);
        request.addBody("closeReplay",0);
        request.addBody("closeShare",0);
        request.addBody("closeKf",1);
        try {
            Response response = HttpClient.post(request);
            System.out.println(response.getBodyStr());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(teaRoomService.insertTeaRoom(teaRoom));
    }

    /**
     * 修改直播间信息
     */
    @RequiresPermissions("tea:room:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TeaRoom teaRoom = teaRoomService.selectTeaRoomById(id);
        mmap.put("teaRoom", teaRoom);
        return prefix + "/edit";
    }

    /**
     * 修改保存直播间信息
     */
    @RequiresPermissions("tea:room:edit")
    @Log(title = "直播间信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeaRoom teaRoom) {
        return toAjax(teaRoomService.updateTeaRoom(teaRoom));
    }

    /**
     * 删除直播间信息
     */
    @RequiresPermissions("tea:room:remove")
    @Log(title = "直播间信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teaRoomService.deleteTeaRoomByIds(ids));
    }
}
